package com.itheima.service;

import com.itheima.admin.dishpojo.Dish;
import com.itheima.admin.dishpojo.Flavors;

import com.itheima.admin.AdminPage;
import com.itheima.common.SnowFlake;
import com.itheima.mapper.admin.DishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wuzhiyong
 */
@Service
public class DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SnowFlake snowFlake;

    /**
     * 分页查询
     *
     * @param pageSize pageSize
     * @param name     name
     * @param page     page
     * @param type     type
     * @return adminPage
     */
    public AdminPage<Dish> getPageDish(String name, Integer page, Integer pageSize, Integer type) {
        //获取startIndex
        int startIndex = (page - 1) * pageSize;
        //获取总条数
        int total = dishMapper.selectAllDish();
        //获取分页后内容
        List<Dish> selectpagedh = dishMapper.selectPageDish(name, startIndex, pageSize, type);
        //组装响应实体并返回
        AdminPage<Dish> adminPage = new AdminPage<>();
        adminPage.setSize(pageSize);
        adminPage.setCurrent(page);
        adminPage.setTotal(total);
        adminPage.setRecords(selectpagedh);
        return adminPage;
    }

    /**
     * 菜品下拉列表
     *
     * @param categoryId categoryId
     * @return List<Dish>
     */
    public List<Dish> getDishByCategoryId(String categoryId) {
        return dishMapper.selectByCategoryId(categoryId);
    }

    /**
     * 根据id查询菜品信息
     *
     * @return Dish
     */
    public Dish getDishtByDishId(String id) {
        return dishMapper.selectByDishId(id);
    }

    /**
     * 新增菜品
     *
     * @return 条数>0
     */
    public boolean addDish(Dish dish) {
        //通过雪花算法生成id
        long id = snowFlake.nextId();
        //设置菜品id
        dish.setId(String.valueOf(id));

        List<Flavors> flavors = dish.getFlavors();

        if (flavors == null) {
            return false;
        }
        for (Flavors flavor : flavors) {
            if (flavor == null) {
                continue;
            }
            //设置口味相关联的菜品id
            flavor.setDishId(String.valueOf(id));
            dishMapper.insertFlavors(flavor);
        }

        return dishMapper.insertDish(dish) > 0;
    }

    /**
     * 修改菜品
     *
     * @return 条数>0
     */
    public boolean modifyDish(Dish dish) {
        //获取菜品Id
        String id = dish.getId();
        dishMapper.deleteFlavors(id);
        List<Flavors> flavors = dish.getFlavors();
        if (flavors == null) {
            return false;
        }
        for (Flavors flavor : flavors) {
            if (flavor == null) {
                continue;
            }
            flavor.setDishId(id);
            dishMapper.insertFlavors(flavor);
        }
        return dishMapper.updateDish(dish) > 0;
    }

    /**
     * 删除菜品
     *
     * @return 条数>0
     */
    public boolean removeByIds(String[] ids) {
        if (ids == null) {
            return false;
        }
        for (String id : ids) {
            if (id == null) {
                continue;
            }
            dishMapper.deleteFlavors(id);
        }
        return dishMapper.deleteByIds(ids) > 0;
    }

    /**
     * 修改启停售
     * @param status status
     * @param ids ids
     * @return 条数>0
     */
    public boolean modifyByStatus(Integer status, String[] ids) {
        return dishMapper.updateByStatus(status, ids) > 0;
    }


}
